<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../js/vue.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        #app {
            width: 90%;
            margin: 20px auto;
        }

        input[type="text"], input[type="button"] {
            padding: 10px;
            font-size: 16px;
        }

        #btn {
            background-color: #337ab7;
            color: #fff;
            border: none;
            cursor: pointer;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            padding: 10px;
            text-align: center;
        }

        th {
            background-color: #337ab7;
            color: #fff;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #e5e5e5;
        }
    </style>
</head>

<body>
<div id="app">
    <a href="addBook.html"><input type="button" value="新增"> </a>
<!--    <form action="/QueryBookServlet" method="post">-->
        <input type="text" placeholder="输入作者或书名查找" name="queryName" v-model="queryName">
        <input type="button" id="btn" value="查找" v-on:click="submitQuery"/>
<!--    </form>-->
    <br>
    <hr>
    <table id="booksTable" class="bookTable" border="1" cellspacing="0" width="90%" align="center">
        <tr>
            <th>序号</th>
            <th>书名</th>
            <th>作者</th>
            <th>简介</th>
            <th>操作</th>
        </tr>
        <tr v-for="(book, i) in books" align="center">
            <td>{{i+1}}</td>
            <td>{{book.bookName}}</td>
            <td>{{book.writer}}</td>
            <td align="left">{{book.description}}</td>
            <td>
                <a v-bind:href="book.updateUrl" >修改</a>
                <a v-bind:href="book.delUrl" >删除</a>
            </td>
        </tr>
    </table>
</div>

</body>
<script>
    new Vue({
        el:"#app",
        data:function () {
            return {
                books:[]
            }
        },
        mounted:function () {
            var _this = this;
            axios({
                method:"post",
                url:"http://localhost:8080/BigHomeworkProject/SelectBookServlet"
            }).then(resp => {
                _this.books = resp.data;
                for(var i = 0; i < _this.books.length; i++) {
                    var book = _this.books[i];
                    book.updateUrl = "../updateBookServlet?id=" + book.id;
                };
                for(var i = 0; i < _this.books.length; i++) {
                    var book = _this.books[i];
                    book.delUrl = "../deleteBookServlet?id=" + book.id;
                };
            })
        },

        methods: {
            submitQuery() {
                var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/BigHomeworkProject/QueryBookServlet",
                    data: _this.books,
                    data: _this.queryName
                }).then(res => {
                        _this.books = res.data;
                        window.location.href = "http://localhost:8080/BigHomeworkProject/html/library.html";
                });
            }
        }
    });
</script>
</html>